package com.ogilvy.audi.webapp.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.ogilvy.audi.model.CircleUser;
import com.ogilvy.audi.model.CircleUserExt;
import com.ogilvy.audi.model.util.PageParameter;
import com.ogilvy.audi.service.CircleUserManager;
import com.ogilvy.audi.service.GenericManager;
import com.ogilvy.audi.util.ServiceDispatcher;
import com.ogilvy.audi.webapp.action.page.PageAction;
import com.ogilvy.audi.webapp.util.PageConstants;
import com.opensymphony.xwork2.Preparable;

public class CircleUserAction extends PageAction implements Preparable {
    private CircleUserManager circleUserManager;
    private ServiceDispatcher serviceDispatcher;
    private List circleUsers;
    private CircleUser circleUser;
    private CircleUserExt circleUserExt;
    
    private Long seqid;
    private String query;

    private String name;    //页面查询参数，对应生成get set方法
    
    public void setCircleUserManager(CircleUserManager circleUserManager) {
        this.circleUserManager = circleUserManager;
    }

    public List getCircleUsers() {
        return circleUsers;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String circleUserId = getRequest().getParameter("circleUser.seqid");
            if (circleUserId != null && !circleUserId.equals("")) {
                circleUser = circleUserManager.get(new Long(circleUserId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        searchCondition(null);
        //circleUsers = circleUserManager.search(query, CircleUser.class);
        return SUCCESS;
    }

    public void setSeqid(Long seqid) {
        this.seqid = seqid;
    }

    public CircleUser getCircleUser() {
        return circleUser;
    }

    public void setCircleUser(CircleUser circleUser) {
        this.circleUser = circleUser;
    }

    public String delete() {
        circleUserManager.remove(circleUser.getSeqid());
        saveMessage(getText("circleUser.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (seqid != null) {
            circleUser = circleUserManager.get(seqid);
        } else {
            circleUser = new CircleUser();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (circleUser.getSeqid() == null);
        	log.debug("The vehicleowner is "+circleUser.getCircleUserExt().getVehicleOwner());
        	getManager().save(circleUser);
        //circleUserManager.save(circleUser);

        String key = (isNew) ? "circleUser.added" : "circleUser.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }


    /**
     * 分页查询
      * @return
     */


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

   @Override
    public String getTableId() {
        return "circleUserList";  //页面返回LIST名称
    }

   @Override
    public GenericManager getManager() {
        return 	serviceDispatcher.getCircleUserManager();        //分页使用manager
    }

    /**
     * 页面参数
     * @return
     */
      @Override
      public Map getSearchCondition(){
        log.debug("Entering  getSearchCondition method");
	      Map condition = PageConstants.initConditionMap();
	      List parList=(List)condition.get(PageConstants.DEFAULT_PARA_NAME);    //一般参数
	      List dateList=(List)condition.get(PageConstants.DEFAULT_DATE_NAME);  //日期参数
	      List sortList=(List)condition.get(PageConstants.DEFAULT_SORT_NAME);//排序
	      
        if(StringUtils.isNotBlank(name)){
           PageParameter par=new PageParameter();
            par.setParName("name");  //对象属性名
            par.setParValue(name);
            par.setFuzzyStatus(true);//是否模糊查询 模糊查询使用like,精确查询使用=
            parList.add(par);
        }

        if(StringUtils.isNotBlank(startDate)){
            PageParameter date=new PageParameter();
            date.setParName("visitTime");   //对象属性名
            date.setParValue(startDate);    //页面传入日期值
            date.setCompareSymbol(">=");  //比对符号
            dateList.add(date);
        }

        if(StringUtils.isNotBlank(endDate)){
            PageParameter date=new PageParameter();
            date.setParName("visitTime");
            date.setParValue(endDate);
            date.setCompareSymbol("<=");
            dateList.add(date);
        }

       return PageConstants.condition;
    }

    public Long getSeqid() {
        return seqid;
    }

    public void setCircleUsers(List circleUsers) {
        this.circleUsers = circleUsers;
    }

    public CircleUserExt getCircleUserExt() {
        return circleUserExt;
    }

    public void setCircleUserExt(CircleUserExt circleUserExt) {
        this.circleUserExt = circleUserExt;
    }
    
    
      
    
}